<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\ParkInfo;
use App\Models\ParkRecord;
use App\Models\Report;

class FlowCountFix extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'flow:count-fix';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '流量统计-fix';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start = Carbon::yesterday()->timestamp;

        $end = Carbon::today()->timestamp;

        // print_r(config('custom.report_flow_parks'));
        // return;

        //$parks = ParkInfo::whereIn('park_code', config('custom.report_flow_parks'))
        $parks = ParkInfo::whereIn('park_code', [7100000290])
            ->select(['park_code', 'parking_name'])
//            ->where('park_code', '7100000226')
            ->orderBy('sort_by', 'desc')
            ->get();
        $taxi_park_code = config('custom.taxi_parks');
        $data = $parks->map(function ($item, $key) use ($start, $end, $taxi_park_code) {
            //生成报表列
            $rowKey = 'time_' . $item->park_code;
            $row = [
                $rowKey . '_0_15_free' => 0,
                $rowKey . '_15_30_free' => 0,
                $rowKey . '_0_15_charge' => 0,
                $rowKey . '_15_30_charge' => 0,
                $rowKey . '_free' => 0,
                $rowKey . '_charge' => 0,
                $rowKey . '_total' => 0,
                $rowKey . '_greater_1440_free' => 0,
                $rowKey . '_greater_1440_charge' => 0
            ];

            for ($i = 30; $i < 1440; $i += 30) {
                $endTIme = $i + 30;
                $row[$rowKey . "_{$i}_{$endTIme}_free"] = 0;
                $row[$rowKey . "_{$i}_{$endTIme}_charge"] = 0;
            }


            //查询前一天数据
            $table = 't_park_record_' . $item->park_code;
            $parkRecord = new ParkRecord();
            $parkRecord->setTable($table);
            if(!in_array($item->park_code, $taxi_park_code)) {
                $res = $parkRecord
                    ->where('out_time', '>=', $start)
                    ->where('out_time', '<', $end)
                    ->select(['parking_duration', 'parking_charge'])->get();
            } else {
                $res = $parkRecord
                    ->where('out_time', '>=', $start)
                    ->where('out_time', '<', $end)
                    ->where('parking_charge', '<>', 500)
                    ->select(['parking_duration', 'parking_charge'])->get();
            }

            foreach ($res as $value) {
                //定位时间范围
                $filed = $this->initFiled($value, $rowKey);
                //收费车或免费车
                if ($value->parking_charge < 1) {
                    $row[$rowKey . '_free'] += 1;
                    if (isset($row[$filed . '_free'])) {
                        $row[$filed . '_free'] += 1;
                    } else {
                        '';
                    }
                } else {
                    if (isset($row[$filed . '_charge'])) {
                        $row[$filed . '_charge'] += 1;
                    } else {
                        '';
                    }
                    $row[$rowKey . '_charge'] += 1;
                }

                $row[$rowKey . '_total'] += 1;
            }
            return $row;
        });


        $data = json_decode($data->collapse(), true);
        foreach($data as $k=>&$v){
            if(strpos($k, "7100000288")){
                $k30 = str_replace("7100000288","7100000290", $k);
                $k3 = str_replace("7100000288","7100000291", $k);
                $data[$k] = ($data[$k]-$data[$k30]-$data[$k3])>0?$data[$k]-$data[$k30]-$data[$k3]:0;
            }
        }
        print_r($data);
        return;   

        // //存入数据库
        // $report = new Report();
        // $report->report_date = Carbon::yesterday()->format('Y-m-d');
        // $report->type = 'flow_count_day_report';
        // $report->value = $data->collapse();
        // $report->save();

        // $this->info('流量统计成功');



        //存入数据库
        $report_date = Carbon::yesterday()->format('Y-m-d');
        $report_type = 'flow_count_day_report';
        $report = Report::where('type', $report_type)
                            ->where('report_date', $report_date)
                            ->first();
        if (!$report) {
            $report = new Report;
            $report->report_date = $report_date;
            $report->type = $report_type;
        }

        $report->value = $data->collapse();
        $report->save();

        $this->info('流量统计成功');

    }

    /**
     * 计算时间范围
     *
     * @param $value
     * @param $key
     * @return string
     */
    private function initFiled($value, $key)
    {
        $time = ceil($value->parking_duration / 60);
        if ($time > 0 && $time <= 15) return "{$key}_0_15";
        if ($time > 15 && $time <= 30) return "{$key}_15_30";

        for ($i = 30; $i < 1440; $i += 30) {
            if ($time > $i && $time <= ($i + 30)) return "{$key}_" . $i . "_" . ($i + 30);
        }
        if($time>1440) return $key . "_greater_1440";
    }
}
